package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author ct_start
 * @since 2021-08-12
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    VodClient vodClient;

    @Override
    public List<EduVideo> FindAllByChapterId(String chapterId) {
        // 根据课程ID 查询所有的小节
        QueryWrapper<EduVideo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        List<EduVideo> list = baseMapper.selectList(queryWrapper);

        return list;
    }


    // 根据课程Id 删除小节
    // 删除小节的时候还需要删除对应的视频文件

    @Override
    public void removeChapterByCourseId(String courseId) {
        // 根据课程Id 查询课程的所有视频id
        QueryWrapper<EduVideo> queryWrapper1=new QueryWrapper<>();
        // 可以指定查询列 ,而不是查询出所有的列
        queryWrapper1.eq("course_id",courseId);
        queryWrapper1.select("video_source_id");
        List<EduVideo> list = baseMapper.selectList(queryWrapper1);
        // 传进删除所有视频的方法是传一个String 类型的字符串,这里还需要类型转换
        List<String> list1=new ArrayList<>();
        for (EduVideo eduVideo : list) {

            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
            list1.add(videoSourceId);}
        }

        // 再调用vod 微服务里面删除多个视频的方法
        if (list1.size()>0) {
            vodClient.deleteBatch(list1);
        }
        QueryWrapper<EduVideo> queryWrapper=new QueryWrapper();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
